//package eg.ci.mvn.service.core.config.swagger;
//
//import cn.hutool.extra.spring.SpringUtil;
//import eg.ci.mvn.service.core.config.git.GitRepositoryState;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.beans.factory.config.BeanPostProcessor;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.util.ReflectionUtils;
//import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
//import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
//import springfox.documentation.service.Contact;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
//import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
//
//import java.lang.reflect.Field;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// *
// * 服务编号(A-Z)+控制层编号(01-99)+接口编号(01-99)
// */
//@Slf4j
//@Configuration
//@Import(BeanValidatorPluginsConfiguration.class)
//@EnableSwagger2WebMvc
//public class SwaggerV2Config implements WebMvcConfigurer {
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//    }
//
//    /** 请求URL都会加上 pathMapping 前缀 */
//    @Value("${swagger.path-mapping:}")
//    private String pathMapping;
//
//    @Bean
//    public Docket createRestApi() {
//        log.info("swagger.api.create: {}", pathMapping);
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo()).enable(true)
//                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.any())
//                .build()
//                // 请求URL都会加上 pathMapping 前缀
//                .pathMapping(pathMapping);
//    }
//
//    private ApiInfo apiInfo() {
//        String custVersionName = SpringUtil.getBean(GitRepositoryState.class).getCustVersionName();
//        return new ApiInfoBuilder()
//                .title("xxx服务接口规范")
//                .description("xxx公司xxx服务接口开发文档")
//                .termsOfServiceUrl("http://group.xxx.cn")
//                .contact(new Contact("XXX-技术开发组", "", ""))//作者
//                .version(custVersionName)
//                .build();
//    }
//
//
//    /**
//     * https://blog.csdn.net/u011410254/article/details/127586555
//     * @return
//     */
//    @Bean
//    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
//        return new BeanPostProcessor() {
//
//            @Override
//            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//                if (bean instanceof WebMvcRequestHandlerProvider) {
//                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
//                }
//                return bean;
//            }
//
//            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
//                List<T> copy = mappings.stream()
//                        .filter(mapping -> mapping.getPatternParser() == null)
//                        .collect(Collectors.toList());
//                mappings.clear();
//                mappings.addAll(copy);
//            }
//
//            @SuppressWarnings("unchecked")
//            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
//                try {
//                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
//                    field.setAccessible(true);
//                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
//                } catch (IllegalArgumentException | IllegalAccessException e) {
//                    throw new IllegalStateException(e);
//                }
//            }
//        };
//    }
//}
//
